package oop.controller.action.question;

import java.util.List;

import oop.controller.action.DefaultAction;
import oop.data.BaseQuestion;
import oop.db.dao.BaseQuestionDAO;

public class ViewAction extends DefaultAction {
	@Override
	public void perform() throws Exception {
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			List<BaseQuestion> questions = BaseQuestionDAO.fetchById(id);

			if (questions.isEmpty()) {
				request.setAttribute("message",
						"Không tìm thấy câu hỏi có mã số " + id);
				setNextAction("error");
			} else {
				BaseQuestion question = questions.get(0);
				request.setAttribute("question", question);
			}
		} catch (NumberFormatException ex) {
			request.setAttribute("message", "Id không hợp lệ: "
					+ request.getParameter("id"));
			setNextAction("error");
		}
	}
}
